<?php if(!defined('SPRITCH')) { exit(); }

// Spritch - An opensource PHP5 web framework

// Author: Luca Degasperi
// Licence: GNU GPL
// Site: http://www.spritch.org/

// Class: Html
// Summary: This class provides simplified html interface
// Since: 0.9.1
 
class html_helper extends helper
{

	public function __construct()
	{
	}

	public function a($url = null,$content = '',$attributes = array(),$protocol = null)
	{
		$trueurl = $this->href($url,$protocol);
		if($content === '')
			$content = $trueurl;
		$attr = $this->assoc_to_attr($attributes);
		return '<a href="'.$trueurl.'" '.$attr.'>'.$content.'</a>'."\n";
	}

	public function pa($protocol,$url = null,$content = '',$attributes = array())
	{
		return $this->a($url,$content,$attributes,$protocol);
	}

	public function img($src = null,$alt = 'image',$attributes = array())
	{
		if(strpos($src,'://') === false)
			$src = $this->src(IMAGES.DS.$src);
		$attr = $this->assoc_to_attr($attributes);
		return '<img src="'.$src.'" alt="'.$alt.'" '.$attr.'/>'."\n";
	}

	public function mailto($email,$content = '',$attributes = array())
	{
		$email = $this->safe_mail($email);
		if($content === '')
			$content = $email;
		$atrr = $this->assoc_to_attr($attributes);
		return '<a href="&#109;&#097;&#105;&#108;&#116;&#111;&#058;'.$email.'" '.$attr.'>'.$content.'</a>'."\n";
	}

	public function link($url,$rel,$type = null,$media='all')
	{
		if(strpos($url,'://') === false)
			$url = $this->src($url);
		return '<link href="'.$url.'" rel="'.$rel.'" type="'.$type.'" media="'.$media.'" />'."\n";
	}

	public function meta($name,$content,$httpequiv = false)
	{
		if($httpequiv === true)
			return '<meta http-equiv="'.$name.'" content="'.$content.'" />'."\n";
		return '<meta name="'.$name.'" content="'.$content.'" />'."\n";
	}

	public function style($style,$media = 'all')
	{
		$style = rtrim($style,'.css').'.css';
		if(strpos($style,'://') === false)
			$style = $this->src(STYLES.DS.$style);
		return '<link href="'.$style.'" rel="stylesheet" type="text/css" media="'.$media.'"/>'."\n";
	}

	public function script($script)
	{
		$script = rtrim($script,'.js').'.js';
		if(strpos($script,'://') === false)
			$script = $this->src(SCRIPTS.DS.$script);
		return '<script type="text/javascript" src="'.$script.'"></script>'."\n";
	}
	
	private function assoc_to_attr($assoc = false)
	{
		$attr = '';
		if(is_array($assoc))
			foreach($assoc as $key => $value)
				$attr .= $key.'="'.$value.'" ';
		return $attr;
	}

	public function href($url = null,$protocol = null)
	{
		$prefix = $this->uri->base();
		if(is_null($url))
			return $prefix;
		else if(strpos($url,'://') === false)
		{
			$proto = '';
			if(!is_null($protocol))
				$proto = $protocol.'://'. str_replace('http://','',$this->uri->url());
			$symbol = DS;
			if(strpos($url,'#') !== false)
				$symbol = substr($url,strpos('#',$url),count($url)-1);
			if($proto !== '')
				return $proto.$url.$symbol;
			else
				return $prefix.$url.$symbol;
		}
		else
			return $url;
	}

	public function lang($lang)
	{
		$uri = explode("/",$this->uri->uri_string());
		if(file_exists(LANGUAGES.DS.$uri[1]) or file_exists(LANGUAGES.DS.$uri[1]))
			$uri = array_slice($uri,2);
		
		return $this->uri->base(false).$lang.DS.implode("/",$uri);		
	}

	private function safe_mail($email)
	{
		$safe = '';
		$chars = str_split($email);
		foreach($chars as $letter)
			switch(rand(1,2))
			{
				case 1: $safe .= '&#'.ord($letter).';'; break;
				case 2: $safe .= '&#x'.dechex(ord($letter)).';'; break;
				case 3: $safe .= $letter;
			}
		return $safe;
	}

	public function src($url)
	{
		return $this->uri->url().$url;
	}
}
?>